package com.masterclient.app.modules.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.order.StorageOrderListData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.order.adapter.StorageOrderAdapter;
import com.masterclient.app.modules.order.contract.StorageOrderListContract;
import com.masterclient.app.modules.order.contract.StorageOrderListPresenter;
import com.masterclient.app.widget.TitlebarView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/10/19.
 */
@Route(path = AroutePath.STORAGE_ORDER_LIST_ACTIVITY)
public class StorageOrderListActivity extends BaseActivity<StorageOrderListPresenter> implements StorageOrderListContract.View {

    @BindView(R.id.title_bar)
    TitlebarView titleBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;

    private int page;
    private StorageOrderAdapter storageOrderAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_refresh);
        ButterKnife.bind(this);

        titleBar.setTitle("我的保管单");
        titleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        recyclerView.setPadding(0, ScreenUtils.dp2px(10),0,ScreenUtils.dp2px(20));
        storageOrderAdapter = new StorageOrderAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        storageOrderAdapter.bindToRecyclerView(recyclerView);

        mPresenter.getList(1);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData(1);
            }
        });

        storageOrderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData(page);
            }
        },recyclerView);

        storageOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build(AroutePath.STORAGE_ORDER_DETAIL_ACTIVITY)
                        .withString("order_no",storageOrderAdapter.getData().get(position).getOrder_no())
                        .navigation();
            }
        });

    }

    private void loadData(int page){
        this.page = page;
        mPresenter.getList(page);
    }

    @Override
    public void showFailure(String msg) {
        swipeLayout.setRefreshing(false);
        if (storageOrderAdapter != null)
        storageOrderAdapter.loadMoreFail();
        ToastUtil.showToast(msg);
    }

    @Override
    public void showData(StorageOrderListData listData) {
        if (swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        storageOrderAdapter.loadMoreComplete();
        if (page == 1){
            if (listData.getItems() != null){
                storageOrderAdapter.setNewData(listData.getItems());
            }
        }else {
            storageOrderAdapter.addData(listData.getItems());
        }

        if(listData.getItems() == null || listData.getItems().size()<1 || !listData.isHas_more()){
            storageOrderAdapter.setEnableLoadMore(false);
        }else {
            ++page;
        }
    }
}
